<template>
  <div class="detail">
    <!-- 导航栏 -->
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="nav"></detail-nav-bar>
    <scroll
      class="content"
      ref="scroll"
      @scroll="contentScroll"
      :probeType="3"
    >
      <!-- 轮播图 -->
      <detail-swiper :topImages="topImages"></detail-swiper>
      <!-- 基础数据详情 -->
      <detail-base-info :goods="goods"></detail-base-info>
      <!-- 商品店铺数据详情 -->
      <detail-shop-info :shop="shop"></detail-shop-info>
      <!-- 商品数据详情 -->
      <detail-goods-info :detailInfo="detailInfo" @imageLoad="imageLoad"></detail-goods-info>
      <!-- 商品参数数据详情 -->
      <detail-param-info :paramInfo="paramInfo" ref="params"></detail-param-info>
      <!-- 评论详情 -->
      <detail-comment-info :commentInfo="commentInfo" ref="comment"></detail-comment-info>
      <!-- 推荐数据 -->
      <goods-list :goods="recommends" ref="recommend"></goods-list>
    </scroll>
    <!-- 底部栏 -->
    <detail-bottom-bar @addToCart="addToCart"></detail-bottom-bar>
    <!-- 回顶 -->
    <back-top @click.native="backClick" v-show="isShowBackTop"></back-top>
  </div>
</template>

<script>
import DetailNavBar from './childComps/DetailNavBar'
import DetailSwiper from './childComps/DetailSwiper'
import DetailBaseInfo from './childComps/DetailBaseInfo'
import DetailShopInfo from './childComps/DetailShopInfo'
import DetailGoodsInfo from './childComps/DetailGoodsInfo'
import DetailParamInfo from './childComps/DetailParamInfo'
import DetailCommentInfo from './childComps/DetailCommentInfo'
import DetailBottomBar from './childComps/DetailBottomBar'

import Scroll from "components/common/scroll/Scroll";
import GoodsList from "components/content/goods/GoodsList";
import BackTop from "components/content/backTop/BackTop";

import { getDetail, getRecommend, Goods, Shop, GoodsParam } from "network/detail";
import { debounce } from 'common/utils';

import { mapActions } from 'vuex'
export default {
  name: "Detail",
  components: {
    BackTop,
    Scroll,
    GoodsList,
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    DetailBottomBar
  },
  data() {
    return {
      isShowBackTop: false, //是否显示backtop按钮
      iid: null,
      topImages: [],
      goods: {},
      shop: {},
      detailInfo: {},
      paramInfo: {},
      commentInfo: {},
      recommends: [],
      themeTopYs: [],
      getThemeTopY: null,
    };
  },
  created() {
    // 1.保存传入的iid
    this.iid = this.$route.params.iid

    // 2.根据iid请求详情数据
    getDetail(this.iid).then((res) => {
      const data = res.data.result
      // 1.获取顶部的图片轮播数据
      this.topImages = data.itemInfo.topImages

      // 2.获取商品信息
      this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)

      //3.获取店铺信息
      this.shop = new Shop(data.shopInfo)

      //4.保存商品详情数据
      this.detailInfo = data.detailInfo
      
      //4.获取商品参数信息
     this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)

     //5.获取评论信息
     if (data.rate.list[0]) {
       this.commentInfo = data.rate.list[0]
     }
    })

    //3.请求推荐数据
    getRecommend().then((res)=> {
      const data = res.data.data
      //1.获取推荐数据
      this.recommends = data.list
    })

    //4.给getThemeTopY赋值
    this.getThemeTopY = debounce(()=> {
      this.themeTopYs = [],
      this.themeTopYs.push(0)
      this.themeTopYs.push(this.$refs.params.$el.offsetTop)
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop)
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop)
      this.themeTopYs.push(Number.MAX_VALUE)
      }, 100)
  },
  methods: {
    ...mapActions(['addCart']),

    imageLoad() {
      this.$refs.scroll.refresh()
      this.getThemeTopY()
    },
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 100)
    },
    contentScroll(position) {
      // 1.判断backtop是否显示
      this.isShowBackTop = (-position.y) > 1000;

      const positionY = -position.y
      let length = this.themeTopYs.length;
      for(let i = 0; i < length - 1; i++){
        if(this.currentIndex !== i && (positionY >= this.themeTopYs[i] && positionY < this.themeTopYs[i + 1])){
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }
      // this.listenShowBackTop(position);
    },
    backClick() {
      this.$refs.scroll.scrollTo(0, 0);
    },
    addToCart() {
      // 1.获取购物车信息
      const product = {}
      product.image = this.topImages[0]
      product.title = this.goods.title
      product.desc = this.goods.desc
      product.price = this.goods.realPrice
      product.iid = this.iid

      // 2.将商品添加到购物车里
      this.addCart(product).then(res => {
        this.$toast.show(res, 1500)
      })
    }
  }
}
</script>

<style scoped>
  .detail {
    position: relative;
    height: 100vh;
    z-index: 9;
    background-color: #fff;
  }
  .detail-nav {
    position: relative;
    z-index: 9;
    background-color: #fff;
  }
  .content {
    height: calc(100% - 44px - 49px);
  }
</style>